#@coding utf8
import hashlib
import json
import time
import urllib2

WECHAT_FUWU = {
    "APP_KEY": "wx6945f07f12b0d392",
    "APP_SECRET": "1f2660255228e254f8e9a450def75abd",
    "token": "gaoxiaoit2017",
    "from_username": "zhaobing654"
}


def response_text(touser, content):
    temp_data = """
        <xml>
            <ToUserName><![CDATA[%s]]></ToUserName>
            <FromUserName><![CDATA[%s]]></FromUserName>
            <CreateTime>%s</CreateTime>
            <MsgType><![CDATA[text]]></MsgType>
            <Content><![CDATA[%s]]></Content>
        </xml>
        """
    xml_data = temp_data % (touser, WECHAT_FUWU["from_username"], str(int(time.time())), content)
    return xml_data



def _hashed_key(key):
    return hashlib.md5(key.encode("utf-8")).hexdigest()



def get_access_token_from_url(request):
    the_url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' + WECHAT_FUWU["APP_KEY"] \
              + '&secret=' + WECHAT_FUWU["APP_SECRET"]
    req = urllib2.Request(the_url)
    res = urllib2.urlopen(req)
    res_data = res.read()
    res.close()
    res_dict = json.loads(res_data)
    access_token = res_dict["access_token"]
    return access_token
